package main.java.game.command;

import main.java.game.Battle;
import main.java.game.Dice;
import main.java.game.DiceImpl;

/**
 * @author  ILDiNHo
 */
public class AttackMonster implements BattleCommand {
	/**
	 * @uml.property  name="battle"
	 * @uml.associationEnd  
	 */
	private Battle battle;
	private Integer diceRoll;

	public AttackMonster(Battle b){
		Dice dice= new DiceImpl();
		diceRoll=dice.roll();
		battle=b;
	}
	public void execute() {
		System.out.println("The dice roll was: "+diceRoll);
		if(compareAttackHeroDefenseMonster()>0){
			substractMonsterLife();
			System.out.println("Your hero " + getAssociatedBattle().getHero().getName() + " inflicted damage on the monster: " + getAssociatedBattle().getMonster().getName()+ 
					" and his life points are now: "+battle.getMonster().getLifepoints()+"\n");
		}
		else{
			substractMonsterDefense();
			Integer damage= diceRoll+battle.getHero().getAttack();
			if(damage<0)
				damage=0;
			System.out.println("Your hero " + getAssociatedBattle().getHero().getName() + " reduced the defense of the monster: " 
					+ getAssociatedBattle().getMonster().getName() +" "+ damage+" points, and his defense is now: "+battle.getMonster().getDefense()+" points"+"\n");
		}
	}

	private Integer compareAttackHeroDefenseMonster(){
		Integer heroRoll=this.getAssociatedBattle().getHero().getAttack()+diceRoll;
		Integer monsterDefense=this.getAssociatedBattle().getMonster().getDefense();
		Integer compare=heroRoll.compareTo(monsterDefense);
		return compare;
	}
	private void substractMonsterLife(){
		Integer life=(getAssociatedBattle().getMonster().getLifepoints())-1;
		this.getAssociatedBattle().getMonster().setLifepoints(life);
	}
	private void substractMonsterDefense(){
		if(diceRoll + battle.getHero().getAttack()>=0)
			battle.getMonster().setDefense(battle.getMonster().getDefense()
					-(diceRoll+battle.getHero().getAttack()));
		
		
	}
	public Battle getAssociatedBattle(){
		return battle;
	}
}
